import { myLogin, getInfo } from '@/api/user'
import { Message } from 'element-ui'
import { setToken, setUserInfo } from '@/utils/auth'
const state = {
  userInfo: {},
  userList: []
}
const mutations = {
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_USERLIST({ commit }, userList) {
    state.userList = userList
  }
}
const actions = {
  // 登录
  async MyLogin({ commit }, params) {
    const res = await myLogin(params)
    commit('SET_USERINFO', res.data)
    if (res.success) {
      Message.success('登录成功')
      setToken('asfasvqwvqvqv')
      setUserInfo(res.data)
    }
    return new Promise((resolve, reject) => {
      return resolve(res)
    })
  },
  // 查询用户信息
  async GetUserList({ commit }, params) {
    const res = await getInfo(params)
    commit('SET_USERLIST', res.data)
    if (res.success) {
      Message.success('查询成功')
      setToken('asfasvqwvqvqv')
    }
    return new Promise((resolve, reject) => {
      return resolve(res)
    })
  }
}
const getters = {
  userInfo: state => state.userInfo,
  userList: state => state.userList
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
